# -*-coding:Utf-8 -* 
""" Load bar
"""
import bge, bgl, blf, os
from .box import *
from .textbox import *
from .color import *

class LevelBar(Box):
	
	def __init__(self, parent,x, y, width, height, margin = 0, image = None, background = None):
		Box.__init__(self, parent,x, y, width, height,background)
		self.bar = Box(self,x,y,width,height,image)
		self._level = 0.0
		self._margin = (0,0,0,0)
		self.margin = margin
		
	@property
	def margin(self):
		return self._margin
	@margin.setter	
	def margin(self, value):
		if type(value) in (tuple,list):
			if len(value) == 2:
				self._margin = (round(value[0]),round(value[1]),round(value[0]),round(value[1]))
			elif len(value) == 4:
				self._margin = (round(value[0]),round(value[3]),round(value[1]),round(value[2]))
			else:
				value = round(value[0])
		if type(value) == int:
			self._margin = (value,value,value,value)
		self.refresh_bar()
		
	@property
	def level(self):
		return self._level
	@level.setter
	def level(self, value):
		if type(value) == int:
			value /= 100
		if type(value) == float and not 0.0 <= value <= 1.0:
			value /= 100
		elif type(value) != float:
			return
		if value < 0.0:
			value = 0.0
		if value > 1.0:
			value = 1.0
		self._level = value
		self.refresh_bar()
		
	def refresh_bar(self):
		self.bar.x = self.margin[0]
		self.bar.y = self.margin[1]
		self.bar.h = self.h-(self.margin[1]+self.margin[3])
		self.bar.w = round((self.w-(self.margin[0]+self.margin[2]))*self._level)
		
	
